<?php
/**
 * 数据配置抽象
 *
 * @author maomao.thx@gmail.com
 * @copyright Copyright (c) 2009 Green Shore (http://www.iwgame.com)
 * @version $Id: Abstract.php 1059 2011-11-18 09:26:15Z mmxie $
 */
abstract class IG_Data_Adapter_Abstract
{
    protected $_view;

    protected $_plugins = array();

    /**
     * Zend_Config
     * @var Zend_Config|array
     */
    protected $_config = array();

    /**
     * IG_Data_Source
     * @var IG_Data_Source
     */
    protected $_source;

    public function __construct($config = array())
    {
        foreach (IG_Config::convArray($config) as $key => $val) {
            $this->{'set' . ucfirst($key)}($val);
        }
    }

    abstract public function loadConfig($id);

    public function loadPlugins()
    {
        $this->_eachPlugins();
        $xmlHttpRequest = $this->getRequest()->isXmlHttpRequest() ? 'XmlHttpRequest' : '';
        $method = 'before' . $xmlHttpRequest;
        foreach ($this->getPlugins() as $plugin) {
            if ($plugin->$method() === false)
                return ;
        }

        $this->getSource()->run();

        $method = 'after' . $xmlHttpRequest;
        foreach ($this->getPlugins() as $plugin) {
            if ($plugin->$method() === false)
                return ;
        }
    }

    protected function _eachPlugins()
    {
        if (!isset($this->_config['plugins'])) {
            throw new IG_Exception('未加载配置');
        } else if (!array_key_exists(0, $this->_config['plugins'])) {
            $this->_config['plugins'] = array($this->_config['plugins']);
        }

        foreach ($this->_config['plugins'] as $pluginConfig) {
            $key = (isset($pluginConfig['namespace']) ? $pluginConfig['namespace'] . '_' : '')
                 . $pluginConfig['name'];
            $this->_plugins[$key] = IG_Data_Plugin::factory($pluginConfig);
        }
    }

    public function getPlugins()
    {
        return $this->_plugins;
    }

    /**
     * @return IG_Data_Source
     */
    public function getSource()
    {
        if (!isset($this->_source)) {
            if (empty($this->_config) || !isset($this->_config['source']))
                throw new Exception("未加载配置或未定义键 'source'") ;
            $this->_source = IG_Data_Source::factory($this->_config['source']);
        }
        return $this->_source;
    }

    /**
     * @param Zend_Config|array $config
     */
    public function setConfig($config)
    {
        $this->_config = $config;
        return $this;
    }

    /**
     * @return array
     */
    public function getConfig()
    {
        return $this->_config;
    }

    /**
     * Set the view object
     *
     * @param  Zend_View_Interface $view
     * @return IG_Data_Adapter_Abstract
     */
    public function setView(Zend_View_Interface $view)
    {
        $this->_view = $view;
        $this->_view->addScriptPath(realpath(current($this->_view->getScriptPaths()) . '../templates'));
        return $this;
    }

    /**
     * @return Zend_View_Interface|null
     */
    public function getView()
    {
        if (null === $this->_view) {
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
            $this->setView($viewRenderer->view);
        }

        return $this->_view;
    }

    /**
     * @return Zend_Controller_Request_Http
     */
    public function getRequest()
    {
        if (!isset($this->_request)) {
            $this->_request = Zend_Controller_Front::getInstance()->getRequest();
        }

        return $this->_request;
    }
}